package com.ljy.jymail.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/*
* 同步Feign远程调用请求头丢失的问题
* */
@Configuration
public class JymailFeignConfig {


    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
//                System.out.println("feign远程之前先进行RequestInterceptor方法");
                //RequestContextHolder.getRequestAttributes()：获得当前请求的所有属性，拿到当前请求
                ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (requestAttributes != null){
                    //获取到当前请求
                    if (requestAttributes.getRequest()!=null){
                        //同步请求头数据---cookie
                        template.header("Cookie",requestAttributes.getRequest().getHeader("Cookie"));
                    }
                }
            }
        };

    }
}
